﻿# MUST HAVE
class NodeControl extends UserControl
	constructor:(@attrs) ->		
		@attrs.width = 160
		@attrs.height = 120
		@connectors = []
		super @attrs
		bound = @getBound()
		@rect = new Kinetic.Rect
			x: 0
			y: 0
			width: bound.w
			height: bound.h
			fill: "white"
			stroke: "violet"
			strokeWidth: 1
		
		@group.add @rect
		cx = bound.w / 2
		cy = bound.h / 2
		
		@connectCircle = new Kinetic.Circle
			x: cx
			y: cy
			radius: 50
			stroke: "gray"
			strokeWidth: 1
		
		@group.add(@connectCircle)
		
		circle = new Kinetic.Circle
			x: cx
			y: cy
			radius: 35
			stroke: "violet"
			strokeWidth: 1
			draggable: false
		
		@group.add circle

		circle = new Kinetic.Circle 
			x: cx
			y: cy
			radius: 10
			fill: "violet"
			strokeWidth: 0		
		@group.add circle

		text = new Kinetic.Text
			x: cx
			y: cy
			text: @text
			fontSize: 10
			fontFamily: "Tahoma"
			fontStyle: 'bold'
			textFill: "red"
			align: "center"
			verticalAlign: "middle"
		
		@group.add text

		@group.on "mouseover", =>
			if (!@selected)
				@group.children[0].setStroke('blue')
			@layer.draw()  

  
		@group.on "mouseout", => 
			if (!@selected) 
				@group.children[0].setStroke('violet')
				@layer.draw()  
	setSelected: (@selected) ->
		super @selected
		if (@selected)
			@group.children[0].setStroke('red');
		else
			@group.children[0].setStroke('violet'); 
	typeName: 'NodeControlEx'

class ConnectorControl extends UserControl
	constructor:(@attrs) ->  
		super @attrs
		@x1 = @attrs.x1
		@y1 = @attrs.y1
		@x2 = @attrs.x2
		@y2 = @attrs.y2
		@node1 = @attrs.node1
		@node2 = @attrs.node2
		
		ref = @
		mainShape = new Kinetic.Shape 
			drawFunc: ->				
				context = @getContext()
				context.lineWidth = @attrs.strokeWidth
				context.strokeStyle = @attrs.stroke
				context.beginPath()
				context.moveTo(ref.x1, ref.y1)
				context.lineTo(ref.x2, ref.y2)
				context.closePath()
				context.stroke()

				context.lineWidth = 1
				context.strokeStyle = 'transparent'
				brect = GeometryHelper
					.makeSimpleBound({ x: ref.x1, y: ref.y1 },
					{ x: ref.x2, y: ref.y2 }, this.attrs.strokeWidth + 4)
				context.beginPath()
				context.moveTo(brect.x1, brect.y1)
				context.lineTo(brect.x2, brect.y2)
				context.lineTo(brect.x3, brect.y3)
				context.lineTo(brect.x4, brect.y4)
				context.closePath()
				context.stroke()			
			stroke: "violet"
			strokeWidth: 3
			name: 'BoundRect'		

		mainShape.on 'mouseover', => 
		@group.add mainShape

		circle = new Kinetic.Circle 
			x: @x1
			y: @y1
			radius: 5
			stroke: "violet"
			strokeWidth: 0
			fill: "violet"
			listening: false		

		@group.add circle

		circle = new Kinetic.Circle
			x: @x2
			y: @y2
			radius: 5
			stroke: "violet"
			strokeWidth: 0
			fill: "violet"
			draggable: true
			listening: false
		

		@group.add circle

		
		@creatingCircle = new Kinetic.Circle
			x: this.x2
			y: this.y2
			radius: 8
			stroke: "violet"
			strokeWidth: 1		
			draggable: true
			listening: false

		@setCreatingMode = (@creating) ->			
			if @creating 
				@group.add @creatingCircle
				for item in @group.children					
					item.attrs.listening = false				
			else
				if (@group.children.indexOf @creatingCircle) >= 0
					@group.remove @creatingCircle
				for item in @group.children
					item.attrs.listening = true	

		@setPos2 = (x, y) ->
			@x2 = x
			@y2 = y
			@group.children[2].setX x
			@group.children[2].setY y
			if @creating
				@group.children[3].setX x
				@group.children[3].setY y
			@layer.draw()

		@setPos1 = (x, y) ->
			@x1 = x
			@y1 = y
			@group.children[1].setX x
			@group.children[1].setY y

			@layer.draw()

		@updatePositionByNodes = ->
			if @node1 == undefined || @node2 == undefined
				return        

			cPos1 = @node1.connectCircle.getAbsolutePosition();
			cPos2 = @node2.connectCircle.getAbsolutePosition();
			points = GeometryHelper.getJoinedPointsOfCircleAndLine1 cPos1.x, cPos1.y, 
				@node1.connectCircle.getRadius().x, cPos2.x, cPos2.y
			
			@setPos1 points[0], points[1]

			points = GeometryHelper.getJoinedPointsOfCircleAndLine1 cPos2.x, 
				cPos2.y, @node1.connectCircle.getRadius().x, cPos1.x, cPos1.y

			@setPos2 points[0], points[1]

		@setCreatingMode @attrs.creating ? false

		mainShape.on 'mouseover', =>
			if @selected
				return;
			for item in @group.children
				item.attrs.stroke = 'blue'			

			@layer.draw();

		mainShape.on 'mouseout', =>
			if @selected
				return
			for item in @group.children
				item.attrs.stroke = 'violet'			

			@layer.draw();
	
	setSelected: (@selected)->
		super @selected
		if @selected
			for item in @group.children
				item.attrs.stroke = 'red'
		else 
			for item in @group.children
				item.attrs.stroke = 'violet'	
	typeName: 'ConnectorControlEx'